package cn.bearspark.api.service.func.impl;

import cn.bearspark.api.service.func.ForwardService;
import cn.bearspark.api.util.ParseableDataUtil;
import cn.bearspark.api.vo.func.forward.HappeningReqVO;
import cn.bearspark.common.constant.GlobalConstant;
import cn.bearspark.common.enumeration.ModuleEnum;
import cn.bearspark.common.util.ConvertBeanUtil;
import cn.bearspark.forward.rpc.ForwardRpc;
import cn.bearspark.framework.web.starter.context.BSRequestContext;
import cn.bearspark.happening.dto.happening.ForwardReqDTO;
import cn.bearspark.happening.rpc.HappeningRpc;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author f
 */
@Service
public class ForwardServiceImpl implements ForwardService {

    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private ForwardRpc forwardRpc;
    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private HappeningRpc happeningRpc;

    private final ParseableDataUtil parseableDataUtil;

    public ForwardServiceImpl(ParseableDataUtil parseableDataUtil) {
        this.parseableDataUtil = parseableDataUtil;
    }

    @Override
    public void forwardHappening(HappeningReqVO happeningReqVO) {
        // 保存转发记录
        long userId = BSRequestContext.getUserId();
        boolean recorded = forwardRpc.forwardHappening(userId, happeningReqVO.getQuotedHappeningId());

        ForwardReqDTO forwardReqDTO = ConvertBeanUtil.convert(happeningReqVO, ForwardReqDTO.class);
        forwardReqDTO.setUserId(userId);
        forwardReqDTO.setRecorded(recorded);

        // 发布动态文本
        parseableDataUtil.publish(ModuleEnum.HAPPENING.getCode(),
                () -> happeningRpc.forward(forwardReqDTO), forwardReqDTO);
    }
}
